package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.service.MarketKeywordService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketKeywordServiceImpl implements MarketKeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public List<MarketKeyword> list(String page, String limit, String sort, String order, String keyword, String url) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();

        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }

        if (!StringUtils.isEmpty(url)) {
            criteria.andUrlLike("%" + url + "%");
        }

        marketKeywordExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));

        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        return marketKeywords;

    }

    @Override
    public int delete(String id) {
        // 这是在创建条件对象
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        // 这是为了拼接where
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();

        // criteria是拼接where的 // 拼接到marketKeywordExample里
        criteria.andIdEqualTo(Integer.parseInt(id));
        try {
            //mapper去执行按条件删除
            marketKeywordMapper.deleteByExample(marketKeywordExample);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;// 失败
        }
        return 0;// 成功
    }

    @Override
    public MarketKeyword create(String keyword, String url, Boolean isHot, Boolean isDefault) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andKeywordsLike(keyword);
        int num=(int)marketGoodsMapper.countByExample(marketGoodsExample);

        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        marketKeyword.setIsHot(isHot);
        marketKeyword.setIsDefault(isDefault);
        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setDeleted(false);
        marketKeyword.setSortOrder(num);

        marketKeywordMapper.insert(marketKeyword);

        return marketKeyword;

    }

    @Override
    public MarketKeyword update(Integer id, String keyword, String url, Boolean isHot, Boolean isDefault, Integer sortOrder, String updateTime, Boolean deleted) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andIdEqualTo(id);

        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        marketKeyword.setIsHot(isHot);
        marketKeyword.setIsDefault(isDefault);
        marketKeyword.setSortOrder(sortOrder);
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setDeleted(deleted);

        marketKeywordMapper.updateByExampleSelective(marketKeyword,marketKeywordExample);
        return marketKeyword;
    }
}
